package com.ccs.onlinemusic.controller;
import com.ccs.onlinemusic.mapper.UserMapper;

import com.ccs.onlinemusic.model.User;
import com.ccs.onlinemusic.server.UserServer;
import com.ccs.onlinemusic.tools.Constant;
import com.ccs.onlinemusic.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import static java.lang.Math.log;



@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServer userServer;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    //登录功能1
    @RequestMapping("/login1")
    public ResponseBodyMessage<User> login1(@RequestParam String username,@RequestParam String password
        , HttpServletRequest request){

        User userLogin=new User();

        userLogin.setUsername(username);
        userLogin.setPassword(password);

        User userInfo=userServer.login(userLogin);

        if(userInfo==null){
            return new ResponseBodyMessage<>(-1,"用户名或密码错误",userInfo);
        }else {
           request.setAttribute("USERINFO_SESSION_KEY",userInfo);
            System.out.println(password);
            return new ResponseBodyMessage<>(0,"登录成功",userInfo);
        }
    }

    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username,@RequestParam String password,
                                           HttpServletRequest request){

        //通过用户名查改该用户
        User user=userServer.selectByName(username);

        if(user==null){
            //用户为空的情况
            log.info("用户为空",user);
            System.out.println("请求失败");
            return new ResponseBodyMessage<>(-1,"用户名或密码错误",user);
        }else{
            //用户不为空的情况
            //判断密码 true false
            boolean flg=bCryptPasswordEncoder.matches(password,user.getPassword());
            if(!flg){
                log.info("密码错误",flg);
                System.out.println("用户名或密码错误");
                return new ResponseBodyMessage<>(-1,"用户名或密码错误",user);
            }else{
                request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
                return new ResponseBodyMessage<>(0,"登录成功",user);
            }
        }
    }



















//    @Autowired
//    private UserMapper userMapper;
//
//    //登录功能
//    @RequestMapping("/login")
//    public String login(@RequestParam String username, @RequestParam String password){
//        User userLogin=new User();
//
//        userLogin.setUsername(username);
//        userLogin.setPassword(password);
//
//        User user=userMapper.login(userLogin);
//
//        if(user!=null){
//            System.out.println("登录成功");
//            return "登录成功";
//        }else{
//            System.out.println("登录失败");
//            return "登录失败";
//        }
//
//    }


}
